// pages/redenvelope/giveredenvelope/index.js
const app = getApp() // 引入app
import _request from '../../../utils/request.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl, //全局img路径
    show: false,
    showList: ['普通包', '拼手气', ],
    title: '普通包',
    cursor: 0,
    num: '',
    gross: '',
    rdtitle: '',
    overlayshow: false,
    redbagData: '',
  },


  // 点击展示
  onShowPopup() {
    this.setData({
      show: true
    })
  },

  // 关闭窗口
  onClose() {
    this.setData({
      show: false
    })
  },

  // 获取红包类型
  onChange(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    this.setData({
      title: value
    })
  },

  // 标题输入
  bindinput(e) {
    this.setData({
      cursor: e.detail.cursor,
      rdtitle: e.detail.value
    })
  },

  // 获取红包数量
  getNum(e) {
    let value = this.validateNumber(e.detail.value)
    if (e.detail.value.substr(0, 1) == 0 && value.length == 1) {
      value = value.replace(/[^0-9-]+/, '');
      if (value < 1) {
        value = 1
      }
      this.setData({
        num: value
      })
    } else {
      this.setData({
        num: value > 100 ? 100 : value
      })
    }

  },

  // 获取红包金额
  getGross(e) {
    let value = this.validateNumber(e.detail.value)
    if (e.detail.value.substr(0, 1) == 0 && value.length == 1) {
      value = value.replace(/[^0-9-]+/, '');
      if (value < 1) {
        value = 1
      }
      this.setData({
        gross: value
      })
    } else {
      this.setData({
        gross: value > 200 ? 200 : value
      })
    }
    
  },

  // 过滤
  validateNumber(val) {
    return val.replace(/\D/g, '')
  },
  // 去塞钱
  onGorge() {
    const {
      num,
      gross,
      rdtitle,
      title
    } = this.data
    if (!num || num < 1) {
      return wx.showToast({
        title: '请设置红包数量',
        icon: 'none',
        duration: 1000
      });
    } else if (!gross || gross < 1) {
      return wx.showToast({
        title: '请设置红包金额',
        icon: 'none',
        duration: 1000
      });
    } else if (!rdtitle || rdtitle.length == 0) {
      return wx.showToast({
        title: '请设置红包标题',
        icon: 'none',
        duration: 1000
      });
    } else {
      const data = {
        grantquantity: num,
        pathList: [],
        sendUserId: wx.getStorageSync('userid'),
        singleamount: gross / num,
        title: rdtitle,
        totalamount: gross,
        userId: wx.getStorageSync('userid'),
        wishwords: "",
        type: title == '普通包' ? 0 : 1
      }
      _request.postObj('/redbagShare/create', {
        ...data
      }).then((res) => {
        this.setData({
          redbagData: res.data.data
        })
        this.setData({
          overlayshow: true
        })
      }).catch((error) => {
        console.log(error, 'error');
      })
    }
  },

  // 点击关闭红包页
  onClickHide() {
    this.setData({
      overlayshow: false
    });
  },

  // 查看隐私条款
  toPrivacypolicy(){
    wx.navigateTo({
      url: '/pages/aboutus/privacypolicy/index',
    })
  },

  // 查看用户协议
  toUseragreement(){
    wx.navigateTo({
      url: '/pages/aboutus/useragreement/index',
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})